package com.higold.cloud.ui.adapter

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.View
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.higold.cloud.R
import com.higold.cloud.mvp.model.bean.AchieveDetail
import com.higold.cloud.mvp.model.bean.Data
import com.higold.cloud.ui.activity.AgentDetailActivity
import com.higold.cloud.view.UpdataAPPProgressBar
import java.text.DecimalFormat
import android.widget.TextView
import com.mlsdev.animatedrv.AnimatedRecyclerView
import java.util.*


/**
 *    author : lxz
 *    date   : 2019/8/15 11:32
 *    desc   :
 */
class AchievedAdapter(data: List<MultiItemEntity>) : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder>(data) {

    companion object {
        val TYPE_LEVEL_0 = 0
        val TYPE_LEVEL_1 = 1
    }

    init {
        addItemType(TYPE_LEVEL_0, R.layout.item_achieved)
        addItemType(TYPE_LEVEL_1, R.layout.item_achieved_expend)
    }

    /**
     * 当itemView滑出界面外调用，停止动画，这样就解决了快速滑动导致的问题
     * @param holder
     */
    override fun onViewDetachedFromWindow(holder: BaseViewHolder) {
        super.onViewDetachedFromWindow(holder)

        val progress = (holder.getView<View>(R.id.pg_manager) as UpdataAPPProgressBar)
        progress.clearAnimation()
    }


    override fun convert(holder: BaseViewHolder, item: MultiItemEntity) {
        when (holder.itemViewType) {
            TYPE_LEVEL_0 -> {
                val data = item as Data
                holder.setText(R.id.tv_agent, data.user_name)
                        .setText(R.id.tv_goal, changeFloatToString(data.amount) + "w / "
                                + changeFloatToString(data.mission_amount) + "w本月")
                        .setText(R.id.tv_percent, changeFloatToString(data.rate) + "%")

                holder.getView<TextView>(R.id.tv_agent).isSelected = true

                setProgress(holder, data)

//                holder.itemView.setOnClickListener {
//                    val pos = holder.adapterPosition
//                    if (achieved.isExpanded) {
//                        collapse(pos)
//                    } else {
//                        expand(pos)
//                    }
//                }

                holder.itemView.setOnClickListener {
                    val intent = Intent(mContext, AgentDetailActivity::class.java)
                    intent.putExtra("user_code", data.user_code)
                    intent.putExtra("Agent_Info", data)
                    mContext.startActivity(intent)
                }
            }
            TYPE_LEVEL_1 -> {
                val achieveDetail = item as AchieveDetail
                holder.setText(R.id.tv_classify, achieveDetail.name)
                        .setText(R.id.tv_percent, changeFloatToString(achieveDetail.value) + "%")
//                        .setImageResource(R.id.iv, if (lv1.isExpanded()) R.mipmap.arrow_b else R.mipmap.arrow_r)
                setProgress(holder, achieveDetail)

                holder.itemView.setOnClickListener {
                    val pos = holder.adapterPosition
                }
            }
        }
    }

    private fun changeFloatToString(double: Double): String {
        val decimalFormat = DecimalFormat("##.###")
        return decimalFormat.format(double)
    }

    private fun setProgress(holder: BaseViewHolder, data : MultiItemEntity){

        val progress = (holder.getView<View>(R.id.pg_manager) as UpdataAPPProgressBar)
        progress.progress = 0f

        val rate = if(data.javaClass.name == AchieveDetail::class.java.name){
            val achieveDetail = data as AchieveDetail
            achieveDetail.value.toFloat()
        } else {
            val data1 = data as Data
            data1.rate.toFloat()
        }

        when (rate) {
            in 0..50 -> progress.setLevel(0)
            in 51..100 -> progress.setLevel(1)
            else -> progress.setLevel(2)
        }

        progress.progress = rate
        progress.duration = 1500
        progress.startAnim()
    }

}